/*******************************************************************************

This code file produces Table 4, "Bootstrap Estimates of Citywide Supply Response and Marginal Fiscal Cost."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
* Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"
	
	local n_boot = 200
	local n_boot_gmm = 200
		
********************************************************************************
* Exogenous building features
********************************************************************************	
	
*** Elasticity

	use "$data/clean/cleaned_data.dta", clear
	
	do "$code/modules/bootstrap_programs.do"
	
	bootstrap tmp=r(tmp), reps(`n_boot') seed(1234) cluster(nta) idcluster(nta_boot): boot_elasticity	
	
	matrix mat_elasticity = r(table)
	local elasticity_estimate = mat_elasticity[1,1]
	local elasticity_stderr = mat_elasticity[2,1]
	
	insert_into_file using table4.csv, key(elasticity_estimate) value(`elasticity_estimate') format(%12.2f)
	insert_into_file using table4.csv, key(elasticity_stderr) value(`elasticity_stderr') format(%12.2f) 

*** Marginal fiscal cost

	use "$data/clean/cleaned_data.dta", clear
		
	do "$code/modules/bootstrap_programs.do"
	
	bootstrap tmp=r(tmp), reps(`n_boot') seed(1234) cluster(nta) idcluster(nta_boot): boot_mcost
	
	matrix mat_mcost = r(table)
	local mcost_estimate = mat_mcost[1,1]
	local mcost_stderr = mat_mcost[2,1]
	
	insert_into_file using table4.csv, key(mcost_estimate) value(`mcost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(mcost_stderr) value(`mcost_stderr') format(%12.0fc)
	
*** Average fiscal cost

	use "$data/clean/cleaned_data.dta", clear
		
	do "$code/modules/bootstrap_programs.do"
	
	bootstrap tmp=r(tmp), reps(`n_boot') seed(1234) cluster(nta) idcluster(nta_boot): boot_acost
	
	matrix mat_acost = r(table)
	local acost_estimate = mat_acost[1,1]
	local acost_stderr = mat_acost[2,1]
	
	insert_into_file using table4.csv, key(acost_estimate) value(`acost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(acost_stderr) value(`acost_stderr') format(%12.0fc)
	
********************************************************************************
* Endogenous building features
********************************************************************************	

*** Both

	* Prepare simulated instrument
	
	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
	* Run bootstrap

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
	do "$code/modules/bootstrap_programs.do"
	
	quietly logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, asis
	mat b_init = e(b)
	
	local n_boot_gmm = 200
	bootstrap mcostperunit = r(mcostperunit) acostperunit = r(acostperunit) elast = r(elast), reps(`n_boot_gmm') seed(1234) cluster(nta) idcluster(nta_boot) noisily: boot_all_gmm
	
	matrix res = r(table)
	
	local mcost_estimate = res[1,1]
	local mcost_stderr = res[2,1]
	
	local acost_estimate = res[1,2]
	local acost_stderr = res[2,2]
	
	local elast_estimate = res[1,3]
	local elast_stderr = res[2,3]
	
	insert_into_file using table4.csv, key(mcost_gmm_estimate) value(`mcost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(mcost_gmm_stderr) value(`mcost_stderr') format(%12.0fc)
	
	insert_into_file using table4.csv, key(acost_gmm_estimate) value(`acost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(acost_gmm_stderr) value(`acost_stderr') format(%12.0fc)
	
	insert_into_file using table4.csv, key(elast_gmm_estimate) value(`elast_estimate') format(%12.2fc)
	insert_into_file using table4.csv, key(elast_gmm_stderr) value(`elast_stderr') format(%12.2fc)
					
********************************************************************************
* Create table
********************************************************************************
	
	cat table4.csv

	cap erase "$tables/output/table4.tex"

	cd "$code/modules"
	
	capture table_from_tpl, t("$tables/templates/table4.tex") ///
							r("$tables/estimates/table4.csv") ///
							o("$tables/output/table4.tex") 
			
	capture table_from_tpl, t("$tables/templates/table4.tex") ///
							r("$tables/estimates/table4.csv") ///
							o("$tables_overleaf/table4.tex") 
							
	exit

	

	
	
	
/*
*** Elasticity

	* Prepare simulated instrument
	
	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
	* Run bootstrap

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
	do "$code/modules/bootstrap_programs.do"
	
	quietly logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, asis
	mat b_init = e(b)
	
	bootstrap tmp=r(tmp), reps(`n_boot_gmm') seed(1234) cluster(nta) idcluster(nta_boot) noisily: boot_elasticity_gmm
	
	matrix mat_elasticity = r(table)
	local elasticity_estimate = mat_elasticity[1,1]
	local elasticity_stderr = mat_elasticity[2,1]
	
	insert_into_file using table4.csv, key(elasticity_gmm_estimate) value(`elasticity_estimate') format(%12.2f)
	insert_into_file using table4.csv, key(elasticity_gmm_stderr) value(`elasticity_stderr') format(%12.2f)

*** Marginal fiscal cost

	* Prepare simulated instrument
	
	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
	* Run bootstrap

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
	do "$code/modules/bootstrap_programs.do"
	
	bootstrap tmp=r(tmp), reps(`n_boot_gmm') seed(1234) cluster(nta) idcluster(nta_boot): boot_mcost_gmm
	
	matrix mat_mcost = r(table)
	local mcost_estimate = mat_mcost[1,1]
	local mcost_stderr = mat_mcost[2,1]
	
	insert_into_file using table4.csv, key(mcost_gmm_estimate) value(`mcost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(mcost_gmm_stderr) value(`mcost_stderr') format(%12.0fc)
	
*** Average fiscal cost

	* Prepare simulated instrument
	
	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
	* Run bootstrap

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
	do "$code/modules/bootstrap_programs.do"

	bootstrap tmp=r(tmp), reps(`n_boot_gmm') seed(1234) cluster(nta) idcluster(nta_boot): boot_acost_gmm
	
	matrix mat_acost = r(table)
	local acost_estimate = mat_acost[1,1]
	local acost_stderr = mat_acost[2,1]
	
	insert_into_file using table4.csv, key(acost_gmm_estimate) value(`acost_estimate') format(%12.0fc)
	insert_into_file using table4.csv, key(acost_gmm_stderr) value(`acost_stderr') format(%12.0fc) */
